package com.sky.controller.user;

import com.sky.constant.BusinessConfigConstant;
import com.sky.mapper.BusinessConfigMapper;
import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController("userShopController")
@RequestMapping("/user/shop")
@Api(tags = "店铺相关接口")
public class ShopController {
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private BusinessConfigMapper businessConfigMapper;
    @GetMapping("/status")
    @ApiOperation("获取营业状态")
    public Result<Integer> getStatus(){
        //先从redis中获取 如果获取不到，再从数据库中获取
        Integer status = (Integer) redisTemplate.opsForValue().get(BusinessConfigConstant.BUSINESS_STATUS_Key);
        if (status == null){
            status = businessConfigMapper.selectById(BusinessConfigConstant.BUSINESS_CONFIG_ID).getShopStatus();
            if (status != null){
                redisTemplate.opsForValue().set(BusinessConfigConstant.BUSINESS_STATUS_Key,status);
            }
        }
        return Result.success(status);
    }
}
